<template>
  <el-drawer
    v-model="isVisible"
    :direction="direction"
    :size="dialogWidth"
    :show-close="false"
    :loading="isLoading"
    :close-on-click-modal="showCancel ? false : true"
    :close-on-press-escape="showCancel ? false : true"
  >
    <template #header>
      <h4>{{ title }}</h4>
    </template>

    <template #default>
      <slot />
    </template>

    <template #footer>
      <div style="flex: auto">
        <el-button
          v-if="showCancel"
          @click="cancel"
        >
          取消
        </el-button>
        <slot name="operate"/>
        <el-button
          type="primary"
          :loading="isLoading"
          @click="confirm"
        >
          {{ confirmText }}
        </el-button>
      </div>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>

  import { toRefs, ref, computed } from 'vue';

  const props = defineProps({
    direction: {
      type: String,
      default: 'rtl',
    },
    size: {
      type: String,
      default: 'small',
    },
    confirmText: {
      type: String,
      default: '确定'
    },
    showCancel: {
      type: Boolean,
      default: true
    }
  })
  const { direction, size } = toRefs(props)

  const isVisible = ref(false)
  const title = ref<string>('弹框');
  const isLoading = ref<boolean>(false)
  const dialogWidth = computed(() => (size.value === 'big' ? 960 : size.value === 'middle' ? 720 : 480));

  const emits = defineEmits({
    confirm: null,
    cancel: null,
  });

  // 工具方法
  const open = (str: string) => {
    title.value = str;
    isVisible.value = true;
  };

  const close = () => {
    isVisible.value = false;
  };
  const confirm = () => {
    emits('confirm', close);
  };
  const cancel = () => {
    emits('cancel', close);
  };

  const setLoading = (b: boolean) => isLoading.value = b

  defineExpose({
    open,
    close,
    confirm,
    cancel,
    setLoading
  });
</script>

<style lang="scss"></style>